package com.service.lx;

import java.math.BigDecimal;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dao.lx.ConfirmDAO;
import com.dao.lx.RepairorderDAO;
import com.dao.lx.UserDAO;
import com.entity.Confirm;
import com.entity.Repairorder;
import com.entity.User;
import com.service.ServiceException;


@Service
@Transactional(rollbackFor = Exception.class)
public class ConfirmService {
	@Autowired
	private ConfirmDAO confirmDAO;
	@Autowired
	private UserDAO userDAO;
	@Autowired
	private RepairorderDAO repairorderDAO;
	/**
	 * 添加/修改/逻辑删除
	 * @param confirm
	 * @throws ServiceException
	 */
	public void addAndUpdateConfirm(Confirm confirm) throws ServiceException{
		try {
			//判断用户是否存在
			if(confirm.getUserId()!=null){
				User user=userDAO.findOne(confirm.getUserId());
				if(user!=null){
					confirm.setUser(user);
				}else{
					confirm.setUser(null);
				}
			}
			//判断维修工单是否存在
			if(confirm.getRepairorderId()!=null){
				Repairorder repairorder=repairorderDAO.findOne(confirm.getRepairorderId());
				if(repairorder!=null){
					Confirm confirm3=confirmDAO.findByRepairorder(repairorder);
					if(confirm3!=null){
						confirm.setConfirmId(confirm3.getConfirmId());
					}
					confirm.setRepairorder(repairorder);
				}else{
					confirm.setRepairorder(null);
				}
			}
			Confirm confirm2=confirmDAO.findOne(confirm.getConfirmId());
			if(confirm2==null){
				confirm.setConfirmIfdelete(new BigDecimal(1));
			}
			confirm.setConfirmIntodate(new Date());
			confirmDAO.save(confirm);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	
	/**
	 * 删除
	 * @param confirmId
	 * @throws ServiceException
	 */
	public void deleteConfirm(Long confirmId) throws ServiceException{
		try {
			confirmDAO.delete(confirmId);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 单条查询
	 * @param confirmId
	 * @return
	 */
	public Confirm findOne(Long confirmId) {
		return confirmDAO.findOne(confirmId);
	}

}
